#define ElemType int

typedef struct ListNode List_Node;

#include "common.h"

int main()
{
	return 0;
}

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */

struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2)
{
	size_t len_1 = ListNode_len(l1), len_2 = ListNode_len(l2);
	if (len_1 < len_2) {
		List_Node* t = l1;
		l1 = l2;
		l2 = t;
	}
	List_Node* res = l1;

	int carry = 0;
	List_Node* last;
	for (; l2; last = l1, l1 = l1->next, l2 = l2->next) {
		int val = l1->val + l2->val + carry;
		int num = val % 10;
		carry = val / 10;
		l1->val = num;
	}

	if (!l1 && carry) {
		last->next = malloc(sizeof(List_Node));
		last->next->val = 1;
		last->next->next = 0;
	} else if (l1)
		while (l1) {
			int val = l1->val + carry;
			int num = val % 10;
			carry = val / 10;
			l1->val = num;
			if (!l1->next && carry) {
				l1->next = malloc(sizeof(List_Node));
				l1->next->val = 1;
				l1->next->next = 0;
				carry = 0;
			}
            l1 = l1->next;
		}

	return res;
}
